package com.mytx.mapper;

import com.mytx.pojo.Advertisement;
import com.mytx.pojo.AdvertisementExample;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface AdvertisementMapper {

    /**
     * 根据直播间id 查询当前直播下面有效的广告位
     * 但是排除 uId ，因为在第一页的时候单独对登录用户查询了一次，以此判断用户是否对该直播间是否还有 有效的广告位
     *
     * @param liveRoomId
     * @return
     */
    List<Advertisement> selectAdvertisementByLiveRoomId(@Param("liveRoomId") String liveRoomId, @Param("uId") String uId);

    /**
     * 根据用户id 和直播间Id 查询自己对给定直播间投放的有效广告位，没有到期结算的都是有效广告位
     *
     * @param uId
     * @return
     */
    @ResultMap("BaseResultMap")
    @Select("select * from advertisement  where u_id=#{uId} and r_id=#{liveRoomId}and status = 0 limit 1")
    Advertisement selectMyValidAdvertisement(@Param("liveRoomId") String liveRoomId, @Param("uId") String uId);
    /**
     * 根据直播间id 查询出价最高的处于当前时段的广告位
     *
     * @param liveRoomId
     * @return
     */

    @ResultMap("BaseResultMap")
    @Select("select * from advertisement where r_id=#{liveRoomId}and status = 0 and start_date <= #{minStartDate}  and end_date >= #{maxEndDate} order by amount DESC limit 1 ")
    Advertisement selectMaxAmountAdvertisement(@Param("liveRoomId") String liveRoomId, @Param("minStartDate") Date minStartDate,@Param("maxEndDate") Date maxEndDate);


    /**
     * 减少该广告位的押金金额，当广告位到期的时候，需要以此结算
     *
     * @param amount
     */
    @Select(" UPDATE advertisement  SET deposit =  deposit - #{amount} ,updated= #{updated} WHERE id = #{advertisementId};SELECT deposit FROM advertisement where id = #{advertisementId}")
    BigDecimal reduceDeposit(@Param("advertisementId") String advertisementId, @Param("amount") BigDecimal amount,@Param("updated")Date updated);

    /**
     * 设置广告位
     *
     * @param advertisement
     */
    void setAdvertisement(Advertisement advertisement);
//------------------------------------------------------------------------- 分割线 ----------------------------------------------------------------------------------------------
    int countByExample(AdvertisementExample example);

    int deleteByExample(AdvertisementExample example);

    int deleteByPrimaryKey(String id);

    int insert(Advertisement record);

    int insertSelective(Advertisement record);

    List<Advertisement> selectByExample(AdvertisementExample example);

    Advertisement selectByPrimaryKey(String id);

    int updateByExampleSelective(@Param("record") Advertisement record, @Param("example") AdvertisementExample example);

    int updateByExample(@Param("record") Advertisement record, @Param("example") AdvertisementExample example);

    int updateByPrimaryKeySelective(Advertisement record);

    int updateByPrimaryKey(Advertisement record);

}